package com.lbd.email.conttoller;

import com.lbd.base.controller.BaseController;
import com.lbd.commons.Result;
import com.lbd.commons.ResultState;
import com.lbd.email.bean.EmailTemplate;
import com.lbd.email.service.EmailTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by zzl on 2016/9/29.
 */
@RestController
@RequestMapping("/emailTemplate")
public class EmailTemplateController extends BaseController {
    @Autowired
    EmailTemplateService emailTemplateService;

    /**
     * 保存邮件模板信息
     *
     * @param emailTemplate
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(EmailTemplate emailTemplate) {
        emailTemplateService.save(emailTemplate);
        return new Result(ResultState.SUCCESS, "成功！");

    }

    /**
     * 分页查询邮件模板信息
     *
     * @param page          第几页
     * @param rows          一页多少条
     * @param emailTemplate 查询条件
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(int page, int rows, EmailTemplate emailTemplate) {
        return new Result(ResultState.SUCCESS, "查询成功",
                emailTemplateService.findByPageConditionJpql(page, rows, emailTemplate));

    }

    /**
     * 删除邮件模板信息
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result del(@RequestParam("ids[]") String[] ids) {
        emailTemplateService.del(ids);
        return new Result(ResultState.SUCCESS, "删除成功", "");
    }

    /**
     * 根据ID查询要编辑的邮件模板信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public Result toEdit(String id) {
        return new Result(ResultState.SUCCESS, "加载成功", emailTemplateService.findById(id));
    }

    /**
     * 修改邮件模板信息
     *
     * @param emailTemplate
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result update(EmailTemplate emailTemplate) {
        emailTemplateService.update(emailTemplate);
        return new Result(ResultState.SUCCESS, "更新成功");
    }

}
